﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web;
using System.IO;

namespace MTO.Framework.Common
{
    public static class AssemblyHelper
    {
        /// <summary>
        /// Gets the version of the current loaded assembly in which the specified class is defined.
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="type">A System.Type object representing a class in the assembly.</param>
        /// <returns>The version of the Assembly</returns>
        public static Version BuildVersion(Type type)
        {
            AssemblyName assemblyName = Assembly.GetAssembly(type).GetName();
            string key = string.Format("MTO.Framework.Web.BuildVersion.{0}.Key", assemblyName.Name);

            // Lire la cache pour obtenir le build date
            object cachedBuildVersion = HttpRuntime.Cache.Get(key);

            // Retourner cette valeur si trouvé
            if (cachedBuildVersion != null)
            {
                return (Version)cachedBuildVersion;
            }

            // Si elle n'est pas en cache, alors retourner la version de l'Assembly
            Version buildVersion;

            try
            {
                buildVersion = assemblyName.Version;
            }
            catch
            {
                buildVersion = new Version(0, 0, 0, 0);
            }

            // Mettre la valeur en cache
            HttpRuntime.Cache.Insert(key, buildVersion);

            return buildVersion;
        }

        /// <summary>
        /// Gets the build datetime of the current loaded assembly in which the specified class is defined.
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="type">A System.Type object representing a class in the assembly.</param>
        /// <returns>A build datetime of the Assembly</returns>
        public static DateTime BuildDate(Type type)
        {
            Assembly assembly = Assembly.GetAssembly(type);
            string key = string.Format("MTO.Framework.Web.BuildDate.{0}.Key", assembly.GetName().Name);

            // Lire la cache pour obtenir le build date
            object cachedBuildDate = HttpRuntime.Cache.Get(key);

            // Retourner cette valeur si trouvé
            if (cachedBuildDate != null)
            {
                return (DateTime)cachedBuildDate;
            }

            // Si elle n'est pas en cache, alors retourner la version de l'Assembly
            DateTime buildDate;
            
            try
            {
                buildDate = new FileInfo(assembly.Location).LastWriteTime;
            }
            catch
            {
                buildDate = new DateTime();
            }
            

            // Mettre la valeur en cache
            HttpRuntime.Cache.Insert(key, buildDate);

            return buildDate;
        }
    }
}
